package com.zx.work1.servlet;

import com.zx.work1.bean.Student;
import com.zx.work1.service.StudentService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@WebServlet(name = "AddStudentServlet",urlPatterns = "/addStudent")
public class AddStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        // 1.获取请求中的用户名和密码信息并打印出来
        String studentNum = request.getParameter("studentNum");
        System.out.println("获取到的学号为：" + studentNum);
        String studentName = request.getParameter("studentName");
        System.out.println("获取到的姓名为：" + studentName);
        String studentSex = request.getParameter("studentSex");
        System.out.println("获取到的性别为：" + studentSex);
        String studentData = request.getParameter("studentData");
        System.out.println("获取到的出生日期为：" + studentData);
        String studentMail = request.getParameter("studentMail");
        System.out.println("获取到的邮箱为：" + studentMail);
        String studentRemark = request.getParameter("studentRemark");
        System.out.println("获取到的备注为：" + studentRemark);

        StudentService studentService = new StudentService();
        List<Object[]> studentCheck = studentService.queryStudentServeiece();

        //判断添加的数据是否为空
        if(studentNum.equals("")||studentName.equals("")|| studentSex.equals("")||studentData.equals("")||studentMail.equals("")||studentRemark.equals("")){
            System.out.println("添加的学员失败");
            request.setAttribute("error", "添加学员失败，有数据为空！");
            // 实现服务器跳转  共享request和response对象
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("main.jsp");
            requestDispatcher.forward(request, response);
            return;
        }

        //判断添加的学员学号是否重复
        for(int i=0;i<studentCheck.size();i++) {
            Object[] studentValue = studentCheck.get(i);
            if(studentValue[1] == (Integer.valueOf(studentNum))){
                request.setAttribute("error", "添加学员失败，学号重复！");
                // 实现服务器跳转  共享request和response对象
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("main.jsp");
                requestDispatcher.forward(request, response);
                return;
            }
        }

        Student student = new Student(Integer.valueOf(studentNum),studentName,studentSex,studentData,studentMail,studentRemark);
        Student stu = studentService.addStudentServeiece(student);
        if(stu != null){
            System.out.println("添加成功");
            // 将登录成功的用户信息放入session对象中实现多个请求共享
            List<Object[]> studentList = studentService.queryStudentServeiece();
            request.getSession().setAttribute("originalList", studentList);
            for(Object[] objects: studentList){
                System.out.println(Arrays.toString(objects));
            }
            // 实现客户端跳转
            response.sendRedirect("main.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
